{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cc56c5c0-cb4c-43b1-99ca-15d8920af43b",
   "metadata": {},
   "source": [
    "# Python 科学数据处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "324e8198-bc40-4475-97b4-26aeddbe97d5",
   "metadata": {},
   "source": [
    "## 数据量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7d58eb66-f7f4-4435-83d9-ca56b5da6486",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "810647932926689280\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6ff8728-521e-4bba-b3bf-f75f38bcf789",
   "metadata": {},
   "source": [
    "## Numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a88c0fd3-bc1e-4a7e-8c83-989e9ae82256",
   "metadata": {},
   "source": [
    "### Python 保存数据的方法\n",
    "\n",
    "### list tuple dict set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "521a05dc-bf9b-4091-8bd0-03b14507111f",
   "metadata": {},
   "outputs": [],
   "source": [
    "a=[1,2,\"3\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cca5a164-d68b-43f8-8c0e-729082c81efd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建numpy\n",
    "import numpy as np\n",
    "\n",
    "a=[[1,2,3,4],[5,6,7,8]]\n",
    "b=np.array(a)  \n",
    "type(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "780347e5-4818-4dd5-bb2e-496314362dfc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 4)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.shape  #变量b的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "61325b89-a594-44fd-b539-6777fb78fb5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "6a92672d-352a-4518-9a98-af3abb4539f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n"
     ]
    }
   ],
   "source": [
    "# 创建\n",
    "a=np.arange(16)\n",
    "print(a)\n",
    "\n",
    "# 改变\n",
    "a=a.reshape(4,4)\n",
    "print(a)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2a14a054-515e-49b8-9a83-dfe116882074",
   "metadata": {},
   "outputs": [],
   "source": [
    "#三维\n",
    "a=np.arange(64)\n",
    "a=a.reshape(16,2,2)\n",
    "#print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "56b903da-6cda-4c30-9ea0-51672954ffda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]\n",
      " [24 25 26 27]\n",
      " [28 29 30 31]\n",
      " [32 33 34 35]\n",
      " [36 37 38 39]\n",
      " [40 41 42 43]\n",
      " [44 45 46 47]\n",
      " [48 49 50 51]\n",
      " [52 53 54 55]\n",
      " [56 57 58 59]\n",
      " [60 61 62 63]]\n"
     ]
    }
   ],
   "source": [
    "#切片\n",
    "a=np.arange(64)\n",
    "a=a.reshape(16,4)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "09090c7d-ed92-4e69-960a-8a585726a592",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[21 22]\n",
      " [25 26]\n",
      " [29 30]\n",
      " [33 34]]\n"
     ]
    }
   ],
   "source": [
    "print(a[5:9,1:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "856c66e5-4ccf-4156-830b-d365cb492676",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[20 21 22 23]\n",
      " [24 25 26 27]\n",
      " [28 29 30 31]\n",
      " [32 33 34 35]]\n"
     ]
    }
   ],
   "source": [
    "print(a[5:9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "7ea01853-508c-4d6e-8fdb-1de9b162d176",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[  0   1   2   3]\n",
      "  [  4   5   6   7]\n",
      "  [  8   9  10  11]\n",
      "  [ 12  13  14  15]\n",
      "  [ 16  17  18  19]\n",
      "  [ 20  21  22  23]\n",
      "  [ 24  25  26  27]\n",
      "  [ 28  29  30  31]\n",
      "  [ 32  33  34  35]\n",
      "  [ 36  37  38  39]\n",
      "  [ 40  41  42  43]\n",
      "  [ 44  45  46  47]\n",
      "  [ 48  49  50  51]\n",
      "  [ 52  53  54  55]\n",
      "  [ 56  57  58  59]\n",
      "  [ 60  61  62  63]]\n",
      "\n",
      " [[ 64  65  66  67]\n",
      "  [ 68  69  70  71]\n",
      "  [ 72  73  74  75]\n",
      "  [ 76  77  78  79]\n",
      "  [ 80  81  82  83]\n",
      "  [ 84  85  86  87]\n",
      "  [ 88  89  90  91]\n",
      "  [ 92  93  94  95]\n",
      "  [ 96  97  98  99]\n",
      "  [100 101 102 103]\n",
      "  [104 105 106 107]\n",
      "  [108 109 110 111]\n",
      "  [112 113 114 115]\n",
      "  [116 117 118 119]\n",
      "  [120 121 122 123]\n",
      "  [124 125 126 127]]\n",
      "\n",
      " [[128 129 130 131]\n",
      "  [132 133 134 135]\n",
      "  [136 137 138 139]\n",
      "  [140 141 142 143]\n",
      "  [144 145 146 147]\n",
      "  [148 149 150 151]\n",
      "  [152 153 154 155]\n",
      "  [156 157 158 159]\n",
      "  [160 161 162 163]\n",
      "  [164 165 166 167]\n",
      "  [168 169 170 171]\n",
      "  [172 173 174 175]\n",
      "  [176 177 178 179]\n",
      "  [180 181 182 183]\n",
      "  [184 185 186 187]\n",
      "  [188 189 190 191]]\n",
      "\n",
      " [[192 193 194 195]\n",
      "  [196 197 198 199]\n",
      "  [200 201 202 203]\n",
      "  [204 205 206 207]\n",
      "  [208 209 210 211]\n",
      "  [212 213 214 215]\n",
      "  [216 217 218 219]\n",
      "  [220 221 222 223]\n",
      "  [224 225 226 227]\n",
      "  [228 229 230 231]\n",
      "  [232 233 234 235]\n",
      "  [236 237 238 239]\n",
      "  [240 241 242 243]\n",
      "  [244 245 246 247]\n",
      "  [248 249 250 251]\n",
      "  [252 253 254 255]]]\n"
     ]
    }
   ],
   "source": [
    "a=np.arange(256)\n",
    "a=a.reshape(4,16,4)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "3c1356c2-859c-48a0-8ec7-cb263723b385",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5  6]\n",
      " [13 14]]\n"
     ]
    }
   ],
   "source": [
    "print(a[0,1:4:2,1:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52f8f0a9-4684-4ba2-85c7-ae3bbc4fef37",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "8266c2b5-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Z4FlHgWUOJZQIlnASWeBZR4FlDiWUCJZwElngWUeBZQ4llAiWcBJZ4FlHgWUOJZQIlnASWeBZR4FlDiWUCJZwElngWUeBZQ4llAiWcBJZ4FlHgWUOJZQIlnASWeBZR4FlDiWUCJZwElngWUeBZQ4llAiWcBJZ4FlHgWUOJZQIlnASWeBZR4FlDiWUCJZwElngWUeBZQ4llAiWcBJZ4FlHgWUOJZQIlnASWeBZR4FlDiWUCJZwElngWUeBZQ4llAiWcBJZ4FlHgWUOJZQIlnASWeBZR4FlDiWUCJZwElngWUeBZQ4llAiWcBJZ4FlHgWUOJZQIlnASWeBZR4FlDiWUCJZwElngUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAtgsc9CHteWnZQgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=400x400>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from PIL import Image\n",
    "\n",
    "# im = np.random.randint(0,255, (100,300,3), dtype=np.uint8)\n",
    "def dot(x, y, color=(255,0,0) ):\n",
    "    im[x, y] = color\n",
    "\n",
    "def line(x1, y1, x2, y2, color):\n",
    "    for x in range(x1, x2+1):    #疏散点：先判断x相差大还是y相差大\n",
    "        y = int((x-x1)/(x2-x1)*(y2-y1)+0.5) + y1    #+0.5，产生四舍五入的效果，不会断开。\n",
    "            # 无法画垂直的线，if x1=x2\n",
    "        dot(x,y,color)\n",
    "    \n",
    "im = np.zeros( (400,400,3), dtype=np.uint8)     #3:RGB,如果使用灰度，则（400，400），color=255即可\n",
    "\n",
    "line(10,10,300,300,(255,0,0))     #三个数字的大小为三原色的强度\n",
    "im = Image.fromarray(im)\n",
    "\n",
    "display(im)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "9663b66f-f333-40de-b722-2d1dfb38ed00",
   "metadata": {},
   "outputs": [],
   "source": [
    "#作业：写出一个满足各种条件的line函数。满足上述要求，且竖线横线不断线\n",
    "#完成一个画圆的函数circle（x,y,r,color）。提示：np,numpy的三角函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c7df32b6-92e5-4655-b30c-b13ecec08f4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAIAAAAP3aGbAAAB80lEQVR4nO3MsQkAAAjAMP3/aP3ATRBM1kIjAAAAAAAAAAAAAADgkpxzrd4BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADgtQZtAwECHDOVcwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=400x400>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from PIL import Image\n",
    "\n",
    "def dot(x, y, color=(255,0,0) ):\n",
    "    im[x, y] = color\n",
    "\n",
    "def line(x1, y1, x2, y2, color):\n",
    "    if abs(x2-x1)>=abs(y2-y1):\n",
    "        for x in range(x1, x2+1):\n",
    "            y = int((x-x1)/(x2-x1)*(y2-y1)+0.5) + y1\n",
    "            if y2==y1:\n",
    "                dot(x,y1,color)\n",
    "            dot(x,y,color)\n",
    "    else:\n",
    "        for y in range(y1, y2+1):\n",
    "            x = int((y-y1)/(y2-y1)*(x2-x1)+0.5) + x1\n",
    "            if x2==x1:\n",
    "                dot(x1,y,color)\n",
    "            dot(x,y,color)\n",
    "    \n",
    "\n",
    "    \n",
    "    \n",
    "im = np.zeros( (400,400,3), dtype=np.uint8)     #3:RGB,如果使用灰度，则（400，400），color=255即可\n",
    "\n",
    "line(10,10,10,30,(255,0,0))     #三个数字的大小为三原色的强度\n",
    "im = Image.fromarray(im)\n",
    "\n",
    "display(im)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "30c0a2b9-8296-4c04-9299-f6f9ed31e587",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAIAAAAP3aGbAAADQklEQVR4nO3bQWrDQBBEUSX3v7OzCSYLgz3yaKYqfm8tTC+aT0vg4wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAK7ztXsA/onbswesGu+zRZz3N1JPN2noYXjI5jDsnp7T2/P+L/CZLAwD5oZGtoCr3F74UJXzs8CHWtAU2QImWNkRzQJO2nL1OLWAYRvDoVnAgO3J2D4A0CEkFiFjANFyMpEzCZAorRFp8wApMuuQORVbfO8eAAAGJR8yybOxkguL42goQv6EwCL5OcifkAVcWNS0oGVO4EItIWiZk+u4sAAo0XW2dE3LdC4soIZgATUEC4AGjZ+EGmdmFhcWUEOwgBqCBdQQLKCGYAE1BAuoIVhADcECaggWUEOwACjR9U+XrmmZzoUF1BAsoIZgAdCj5cNQy5xcx4UF1BAsjqPheMmfEFgkPwf5E7KAC4tfyUVIng3YIDkKybMBe2R2IXMqtvBKCECntHMmbR4gS04jciYhhFdCHkgoRcIMQIHb7l5sHwBosjEZagUM2xIOtQLOW5kPqQLeteDqcVgBM13UFKnidV+7B6DJvSxT9mbur/EJrArD3g+NVHGOheG8v69yTzdp6GF4yOYwx9PvUFYNAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPhQP95WZePCE1hsAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=400x400>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from PIL import Image\n",
    "import math\n",
    "\n",
    "def dot(x, y, color=(255,0,0) ):\n",
    "    im[x, y] = color\n",
    "    \n",
    "\n",
    "def cricus(x0,y0,r,color):\n",
    "    for i in range(360):\n",
    "        x=x0+r*math.cos(i/180*math.pi)+0.5\n",
    "        y=y0+r*math.sin(i/180*math.pi)+0.5\n",
    "        dot(int(x),int(y),color)\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "im = np.zeros( (400,400,3), dtype=np.uint8)     #3:RGB,如果使用灰度，则（400，400），color=255即可\n",
    "\n",
    "cricus(200,200,30,(255,0,0))     #三个数字的大小为三原色的强度\n",
    "im = Image.fromarray(im)\n",
    "\n",
    "display(im)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d4f76c2-ad1e-4891-9e54-36eab5155360",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
